package com.ikdong.toodle.activity;

import java.util.Date;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.MenuItem.OnMenuItemClickListener;
import com.ikdong.toodle.R;
import com.ikdong.toodle.model.Folder;
import com.ikdong.toodle.util.FolderManager;

public class FolderAddActivity extends BasicActivity implements View.OnClickListener{
	public static final String OPERATION = "ACTION";
	public static final String FID = "FID";
	public static final long OP_ADD = 0;
	public static final long OP_UPDATE = 1;
	private long operation = 0;
	private EditText name;
	private ImageView privated;
	private ImageView archived;
	private Folder folder;
	private Folder uFolder;
	private FolderManager manager;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
        setContentView(R.layout.folder_edit);
        manager = new FolderManager(this);
        operation = this.getIntent().getExtras().getLong(OPERATION);
        initFolder();
        initView();
        initData();
	}
	
	private void initFolder(){		
		if(OP_ADD == operation){
			folder = new Folder();
			folder.setPrivated(Long.valueOf(0));
			folder.setArchived(Long.valueOf(0));
			folder.setOrder(new Date().getTime());
		}else{
			long id = this.getIntent().getExtras().getLong(FID);
			folder = manager.get(id);
			uFolder = new Folder();
			uFolder.setId(folder.getId());
		}
	}
	
	private void initView(){
		String title = "";
        if(OP_ADD == operation){
        	title = getString(R.string.actionbar_title_folder_add);
        }else{
        	title = getString(R.string.actionbar_title_folder_eidt);
        }        
        getSupportActionBar().setTitle(title);
        
		name = (EditText) this.findViewById(R.id.folder_add_title);
		privated = (ImageView) this.findViewById(R.id.fd_private_check);
		archived = (ImageView) this.findViewById(R.id.fd_archived_check);
		
		this.findViewById(R.id.fa_private_list).setOnClickListener(this);
		this.findViewById(R.id.fa_archived_list).setOnClickListener(this);
		
		if(OP_ADD == operation){
			this.findViewById(R.id.fa_archived_list).setVisibility(View.GONE);
		}else{
			this.findViewById(R.id.fa_archived_list).setVisibility(View.VISIBLE);
		}
		
		EditText folderTitle = (EditText) this.findViewById(R.id.folder_add_title);
		folderTitle.setOnEditorActionListener(new TextView.OnEditorActionListener() {			
			@Override
			public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
				saveFolder();
				return false;
			}
		});
	}
	
	private void initData(){
		name.setText(folder.getName());
		if(Long.valueOf(0).equals(folder.getPrivated())){
			privated.setImageResource(R.drawable.ic_checkbox_empty);
		}else if(Long.valueOf(1).equals(folder.getPrivated())){
			privated.setImageResource(R.drawable.ic_checkbox_full);
		}
		if(Long.valueOf(0).equals(folder.getArchived())){
			archived.setImageResource(R.drawable.ic_checkbox_empty);
		}else if(Long.valueOf(1).equals(folder.getArchived())){
			archived.setImageResource(R.drawable.ic_checkbox_full);
		}
	}

	@Override
	public void onClick(View v) {
		if(R.id.fa_private_list==v.getId()){
			if(folder.getPrivated()==0){
				folder.setPrivated(Long.valueOf(1));
				privated.setImageResource(R.drawable.ic_checkbox_full);
			}else{
				folder.setPrivated(Long.valueOf(0));
				privated.setImageResource(R.drawable.ic_checkbox_empty);
			}
			if(OP_UPDATE == operation) uFolder.setPrivated(folder.getPrivated());
		}else if(R.id.fa_archived_list==v.getId()){
			if(folder.getArchived()==0){
				folder.setArchived(Long.valueOf(1));
				archived.setImageResource(R.drawable.ic_checkbox_full);
			}else{
				folder.setArchived(Long.valueOf(0));
				archived.setImageResource(R.drawable.ic_checkbox_empty);
			}
			if(OP_UPDATE == operation) uFolder.setArchived(folder.getArchived());
		}
	}
	
	@Override
    public boolean onCreateOptionsMenu(Menu menu) {
		getSupportActionBar().setTitle(getString(R.string.actionbar_title_folder_add));
		
		MenuItem cancelItem = menu.add(getString(R.string.lable_action_cancel));
		cancelItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
		cancelItem.setOnMenuItemClickListener(new OnMenuItemClickListener(){
			@Override
			public boolean onMenuItemClick(MenuItem item) {
				Intent intent = new Intent(FolderAddActivity.this,FolderListActivity.class);
				startActivity(intent);
				return true;
			}});
		
		if(OP_UPDATE == operation){
			MenuItem deletelItem = menu.add(getString(R.string.lable_action_delete));
			deletelItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
			deletelItem.setOnMenuItemClickListener(new OnMenuItemClickListener(){
				@Override
				public boolean onMenuItemClick(MenuItem item) {					
					new AlertDialog.Builder(FolderAddActivity.this)
					.setTitle(getString(R.string.msg_alert_folder_delete_title))
					.setMessage(getString(R.string.msg_alert_folder_delete_body))
					.setPositiveButton(getString(R.string.lable_action_ok),
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,int which) {
									manager.deleteByStatus(folder.getId());
									Intent intent = new Intent(FolderAddActivity.this,FolderListActivity.class);
									startActivity(intent);
								}
							})
					.setNegativeButton(getString(R.string.lable_action_cancel),
							new DialogInterface.OnClickListener() {
								public void onClick(DialogInterface dialog,int which) {
								}
							})
					.show();
					return true;
				}});
		}
		
        MenuItem saveItem = menu.add(getString(R.string.lable_action_save));
        saveItem.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM | MenuItem.SHOW_AS_ACTION_WITH_TEXT);
        saveItem.setOnMenuItemClickListener(new OnMenuItemClickListener(){
			@Override
			public boolean onMenuItemClick(MenuItem item) {
				saveFolder();				
				return true;				
			}});
        return true;
    }
	
	private void saveFolder(){
		if(name.getText()==null || name.getText().toString().trim().length()==0){
			showToast(getString(R.string.msg_folder_name_error));
		}else{
			String folderName = name.getText().toString();
			if(!checkRename(folderName)){
				boolean rlt = false;							
				if(OP_ADD == operation){
					folder.setName(folderName);					
					rlt = manager.add(folder);
				}else if(OP_UPDATE == operation){					
					uFolder.setName(folderName);
					rlt = manager.update(uFolder);
				}
				
				Intent intent = new Intent(FolderAddActivity.this,FolderListActivity.class);
				if(!rlt){
					showToast(getString(R.string.msg_error_folder_save));
				}else{
					startActivity(intent);
				}
			}else{
				showToast(getString(R.string.msg_error_folder_rename));
			}
			
			
		}
	}	

	@Override
	void refresh() {
		if(OP_ADD==operation && (manager.get(folder.getId())==null)){
			Intent intent = new Intent(FolderAddActivity.this,FolderListActivity.class);
			startActivity(intent);
		}
	}
	
	private boolean checkRename(String name){
		boolean flag = false;
		if(OP_ADD==operation && (manager.get(name)!=null)){
			flag = true;
		}else{
			if(!name.equalsIgnoreCase(folder.getName()) &&(manager.get(name)!=null)){
				flag = true;
			}
		}
		
		return flag;
	}

	
}



















